package com.mengdie.shuidi.ui.discount.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.mengdie.shuidi.AppContext;
import com.mengdie.shuidi.OkHeader;
import com.mengdie.shuidi.R;
import com.mengdie.shuidi.event.CouponEvent;
import com.mengdie.shuidi.model.BaseModel;
import com.mengdie.shuidi.model.DiscountEntity;
import com.mengdie.shuidi.ui.activity.BaseActivity;
import com.mengdie.shuidi.ui.discount.adapter.DiscountAdapter;
import com.mengdie.shuidi.utils.ToastUtil;
import com.mengdie.shuidi.utils.core.EmptyUtils;

import org.greenrobot.eventbus.EventBus;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 作者：HQY on 2019/4/11 17:57
 * 邮箱：hqy_xz@126.com
 */
public class DiscountActivity extends BaseActivity {


    @Bind(R.id.et_discount_content)
    EditText mEtDiscountContent;
    @Bind(R.id.tv_discount_confirm)
    TextView mTvDiscountConfirm;
    @Bind(R.id.rv_discount_list)
    RecyclerView mRvDiscountList;
    @Bind(R.id.tv_generic_title)
    TextView mTvGenericTitle;
    private List<DiscountEntity> mDiscountList = new ArrayList<>();
    private DiscountAdapter mDiscountAdapter;
    private DiscountEntity mDiscount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_discount);
        ButterKnife.bind(this);
        initView();

    }

    private void initView() {
        mTvGenericTitle.setText("优惠券");
        mDiscountAdapter = new DiscountAdapter(mDiscountList);
        mRvDiscountList.setLayoutManager(new LinearLayoutManager(DiscountActivity.this));
        mRvDiscountList.setAdapter(mDiscountAdapter);
        mRvDiscountList.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {

            }

            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemClick(adapter, view, position);
                mDiscount = mDiscountList.get(position);
                if (mDiscount.getStatus() != -2) {

                    for (int i = 0; i < mDiscountList.size(); i++) {
                        if (i == position) {
                            mDiscountList.get(position).setSelect(true);
                        } else {
                            mDiscountList.get(i).setSelect(false);
                        }
                    }
                    mDiscountAdapter.notifyDataSetChanged();
                    EventBus.getDefault().post(new CouponEvent(mDiscount.getCouponNum()));
                    finish();
                } else {
                    ToastUtil.showToast("该优惠券已失效");
                }
            }

        });
        getCouponList();
    }


    private void getCouponList() {
        OkGo.getInstance().addCommonHeaders(OkHeader.getCommonHeadToken(DiscountActivity.this))
                .post(AppContext.getInstance().getBaseUrl() + "ucenter/coupon_list")
                .tag(DiscountActivity.this)
                .upJson(mapSession())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson gson = new Gson();
                        Type discountType = new TypeToken<BaseModel<List<DiscountEntity>>>() {
                        }.getType();
                        BaseModel<List<DiscountEntity>> disCountList;
                        try {
                            disCountList = gson.fromJson(s, discountType);
                        } catch (JsonSyntaxException e) {
                            disCountList = gson.fromJson(s, BaseModel.class);
                        }
                        if (disCountList.getRet().equals("0")) {

                            mDiscountList = disCountList.getData();
                            mDiscountAdapter.setNewData(mDiscountList);

                        } else {
                            ToastUtil.showToast(disCountList.getMsg());
                        }
                    }
                });
    }


    @OnClick({R.id.rl_generic_back, R.id.tv_discount_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_generic_back:
                finish();
                break;
            case R.id.tv_discount_confirm:
                String content = mEtDiscountContent.getText().toString().trim();
                if (EmptyUtils.isNotEmpty(content)) {
                    EventBus.getDefault().post(new CouponEvent(content));
                    finish();
                } else {
                    ToastUtil.showToast("请输入优惠券");
                }
                break;
            default:
                break;
        }
    }


}
